<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="user.userType != 2">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="showAdd">
          新增
        </el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        label="公司Code"
        align="center"
        prop="companyCode"
        min-width="160"
      />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="网站地址" align="center" prop="websiteUrl" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="联系地址" align="center" prop="contactAddress" />
      <el-table-column label="操作" align="center" width="160">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="showEdit(scope.row)"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="operationType == 'add' ? '添加网站' : '修改网站'"
      v-model="open"
      width="600px"
      append-to-body
    >
      <div class="content">
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
          :size="formSize"
          status-icon
        >
          <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="ruleForm.companyName" maxlength="100" />
          </el-form-item>
          <el-form-item label="联系电话" prop="contactNumber">
            <el-input v-model="ruleForm.contactNumber" maxlength="14" />
          </el-form-item>
          <el-form-item label="联系地址" prop="contactAddress">
            <el-input v-model="ruleForm.contactAddress" maxlength="50" />
          </el-form-item>
          <el-form-item label="网站地址" prop="websiteUrl">
            <el-input v-model="ruleForm.websiteUrl" maxlength="50" />
          </el-form-item>
          <!-- <el-form-item label="公众号wxAppKey" prop="wxAppKey">
            <el-input v-model="ruleForm.wxAppKey" maxlength="100" />
          </el-form-item>
          <el-form-item label="公众号wxAppSecret" prop="wxAppSecret">
            <el-input v-model="ruleForm.wxAppSecret" maxlength="100" />
          </el-form-item> -->
        </el-form>
        <div class="dist_btn">
          <el-button type="primary" plain @click="open = false">取消</el-button>
          <el-button type="primary" @click="saveCompany">确定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="WebsiteConfig">
import {
  insertBasicCompany,
  queryBasicCompany,
  updateBasicCompany,
} from "@/api/website/index";
import { ElMessage } from "element-plus";
import useUserStore from "@/store/modules/user";
let user = useUserStore()
// 列表
let dataList = ref([]);
let loading = ref(false);

function showAdd() {
  open.value = true;
  ruleForm.value = {
    companyName: "",
    contactNumber: "",
    contactAddress: "",
    websiteUrl: "",
    // wxAppKey: "",
    // wxAppSecret: "",
  };
  operationType.value = "add";
}
function showEdit(row) {
  open.value = true;
  ruleForm.value = JSON.parse(JSON.stringify(row));
  operationType.value = "edit";
}
let operationType = ref("add");
let open = ref(false);
let ruleFormRef = ref(null);
let ruleForm = ref({
  companyName: "",
  contactNumber: "",
  contactAddress: "",
  websiteUrl: "",
  // wxAppKey: "",
  // wxAppSecret: "",
});
let companyList = ref([
  { id: 1231321, name: "公司1" },
  { id: 123132121, name: "公司2" },
]);
let rules = ref({
  companyName: [{ required: true, message: "请填写公司名称", trigger: "blur" }],
  contactNumber: [
    { required: true, message: "请填写联系电话", trigger: "blur" },
  ],
  contactAddress: [
    { required: true, message: "请填写公司地址", trigger: "blur" },
  ],
  websiteUrl: [{ required: true, message: "请填写网站地址", trigger: "blur" }],
  // wxAppKey: [
  //   { required: true, message: "请填写公众号appKey", trigger: "blur" },
  // ],
  // wxAppSecret: [
  //   { required: true, message: "请填写公众号wxAppSecret", trigger: "blur" },
  // ],
});
function saveCompany() {
  if (operationType.value == "add") {
    ruleFormRef.value.validate((res) => {
      if (!res) return;
      insertBasicCompany(ruleForm.value).then((res) => {
        open.value = false;
        ElMessage.success("添加成功");
        getList();
      });
    });
  } else {
    ruleFormRef.value.validate((res) => {
      if (!res) return;
      updateBasicCompany(ruleForm.value).then((res) => {
        open.value = false;
        ElMessage.success("修改成功");
        getList();
      });
    });
  }
}
// 查询以及分页
let queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
let total = ref(0);
function getList() {
  loading.value = true;
  queryBasicCompany(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      total.value = res.total;
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
}
getList();
</script>

<style scoped lang="scss">
.content {
  padding: 30px;
  .dist_btn {
    margin-top: 30px;
  }
}
</style>
